package me.sauce.folkpocket48.home

import android.support.design.widget.NavigationView
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
import me.sauce.folkpocket48.R
import me.sauce.folkpocket48.base.BaseActivity
import me.sauce.folkpocket48.data.local.sp.LoginManager
import me.sauce.folkpocket48.home.refresh.RefreshFragment
import me.sauce.folkpocket48.login.LoginActivity
import me.sauce.folkpocket48.utils.replaceFragmentInActivity
import me.sauce.folkpocket48.video.VideoActivity

class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener {
    override var isSimpleToolbar = false
    override fun injectView(): View {
        return initRId(R.layout.activity_main)
    }


    fun initDate() {
        Toast.makeText(this, "123", Toast.LENGTH_SHORT).show()
    }


    override fun initView() {
        super.initView()
        val toggle =
                ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()

        nav_view.setNavigationItemSelectedListener(this)
        setupViewFragment()
        nav_view.getHeaderView(0).findViewById<ImageView>(R.id.nav_header).setOnClickListener {
            if (LoginManager.get().isLogined) {
                LoginActivity.starter(this)
            }
        }
    }


    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        when (item.itemId) {
            R.id.action_settings -> return true
            else -> return super.onOptionsItemSelected(item)
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.
        when (item.itemId) {
            R.id.nav_camera -> {
                VideoActivity.start(this, "http://mp4.48.cn/live/6beb534c-d96e-4bbd-89db-5dfb77cc0c98.mp4")
            }
            R.id.nav_gallery -> {

            }
            R.id.nav_slideshow -> {

            }
            R.id.nav_manage -> {

            }
            R.id.nav_share -> {

            }
            R.id.nav_send -> {

            }
        }

        drawer_layout.closeDrawer(GravityCompat.START)
        return true
    }

    private fun setupViewFragment() {
        supportFragmentManager.findFragmentById(R.id.contentFrame)
                ?: RefreshFragment.newInstance().let { replaceFragmentInActivity(it, R.id.contentFrame) }
    }

}
